using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VTXTIND
    {
        public void SetUpNewRow(ref DataRow dwReturn, DataRow dwInput)
        {
            #region
            #endregion
            switch (app.company)
            {
                default:
                case "YGR":
                case "YCC":
                case "JLT":
                    #region=========
                    dwReturn["TXTIMANDANT"] = app.Mandant;
                    dwReturn["TXTITXTART"] = " ".PadRight(3);
                    dwReturn["TXTISIND"] = " ".PadRight(60);
                    dwReturn["TXTISPRACHKZ"] = " ".PadRight(2);
                    dwReturn["TXTIPIND"] = 0;
                    dwReturn["TXTITITEL"] = " ".PadRight(30);
                    dwReturn["TXTINURERGAENZ"] = " ".PadRight(1);
                    dwReturn["TXTISAPTEXTART"] = " ".PadRight(4);
                    dwReturn["TXTISTARTENDE"] = " ".PadRight(1);
                    dwReturn["TXTIKZPERS"] = " ".PadRight(1);
                    dwReturn["TXTIAENDBEN"] = " ".PadRight(8);
                    dwReturn["TXTIAENDDAT"] = 0;
                    dwReturn["TXTINEUBEN"] = " ".PadRight(8);
                    dwReturn["TXTINEUDAT"] = 0;
                    dwReturn["TXTIAENDZL"] = 0;
                    dwReturn["TXTILPOS"] = 0;
                    dwReturn["TXTIUPDT"] = " ".PadRight(1);
                    dwReturn["TXTIRICHEDIT"] = " ".PadRight(1);
                    dwReturn["TXTIFILLER"] = " ".PadRight(20);
                    #endregion
                    break;
            }
            INTEXDBC.MapData(true, ref dwReturn, dwInput);
        }
        public DataRow Convert2TXTIND(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryTXTIND(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                case "YCC":
                case "TST":
                    #region=========
                    dwReturn["TXTIMANDANT"] = app.Mandant;
                    dwReturn["TXTITXTART"] = " ".PadRight(3);
                    dwReturn["TXTISIND"] = " ".PadRight(60);
                    dwReturn["TXTISPRACHKZ"] = " ".PadRight(2);
                    dwReturn["TXTIPIND"] = 0;
                    dwReturn["TXTITITEL"] = " ".PadRight(30);
                    dwReturn["TXTINURERGAENZ"] = " ".PadRight(1);
                    dwReturn["TXTISAPTEXTART"] = " ".PadRight(4);
                    dwReturn["TXTISTARTENDE"] = " ".PadRight(1);
                    dwReturn["TXTIKZPERS"] = " ".PadRight(1);
                    dwReturn["TXTIAENDBEN"] = " ".PadRight(8);
                    dwReturn["TXTIAENDDAT"] = 0;
                    dwReturn["TXTINEUBEN"] = " ".PadRight(8);
                    dwReturn["TXTINEUDAT"] = 0;
                    dwReturn["TXTIAENDZL"] = 0;
                    dwReturn["TXTILPOS"] = 0;
                    dwReturn["TXTIUPDT"] = " ".PadRight(1);
                    dwReturn["TXTIRICHEDIT"] = " ".PadRight(1);
                    dwReturn["TXTIFILLER"] = " ".PadRight(20);
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapTXTIND(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveTXTIND(DataRow dwData, string ITXDBVERSION = "YGR")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;

            setResult = QueryTXTIND(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapTXTIND(ref dwLocal, dwData);
            if (!flgExist)
            {
                InsertTXTIND(dwLocal);
            }
            UpdateTXTIND(dwLocal);
        }

        public void DTXTIND00(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE FROM TXTIND  "
            + "WHERE  TXTIMANDANT=:TXTIMANDANT: AND TXTITXTART=:TXTITXTART: ";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
        public void DTXTIND01(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand(); 
            or1.CommandText = "DELETE FROM TXTKON WHERE  "
                +"  EXISTS (SELECT 1 FROM TXTIND WHERE  TXTIMANDANT=TXTKMANDANT AND  TXTIPIND=TXTKPIND AND TXTIMANDANT=:TXTIMANDANT: AND TXTITXTART=:TXTITXTART:"+  dwData["TXTISIND"].ToString()+")";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);

            or1.CommandText = "DELETE FROM TXTIND  "
            + "WHERE  TXTIMANDANT=:TXTIMANDANT: AND TXTITXTART=:TXTITXTART: "
            + dwData["TXTISIND"].ToString();
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
        public void InsertTXTIND(DataRow dwData, string ITXDBVERSION = "50.24")
        {

            or1 = OracleDB.CreateCommand();
            switch (app.company)
            {
                default:
                    or1.CommandText = "INSERT INTO TXTIND ( TXTIMANDANT,TXTITXTART,TXTISIND,TXTISPRACHKZ,TXTIPIND,TXTITITEL,TXTINURERGAENZ,TXTISAPTEXTART,TXTISTARTENDE,TXTIKZPERS,TXTIAENDBEN,TXTIAENDDAT,TXTINEUBEN,TXTINEUDAT,TXTIAENDZL,TXTILPOS,TXTIUPDT,TXTIRICHEDIT,TXTIFILLER)"
                    + " VALUES (:TXTIMANDANT:,:TXTITXTART:,:TXTISIND:,:TXTISPRACHKZ:,:TXTIPIND:,:TXTITITEL:,:TXTINURERGAENZ:,:TXTISAPTEXTART:,:TXTISTARTENDE:,:TXTIKZPERS:,:TXTIAENDBEN:,:TXTIAENDDAT:,:TXTINEUBEN:,:TXTINEUDAT:,:TXTIAENDZL:,:TXTILPOS:,:TXTIUPDT:,:TXTIRICHEDIT:,:TXTIFILLER:)";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
            }

            Connect2Proxy(or1);
        }
        public DataSet QueryTXTIND(DataRow dwData, int option = 1)
        {
            DataSet SResult = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT *  FROM TXTIND T WHERE ROWNUM<2";
                    SResult = Connect2Proxy(or1);
                    break;
                case 1:
                    or1.CommandText = "SELECT *  FROM TXTIND T  WHERE TXTIMANDANT=:TXTIMANDANT: AND TXTISIND=:TXTISIND:";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    SResult = Connect2Proxy(or1);
                    break;
                case 2:
                    SResult = QTXTIND02(dwData);
                    break;
                case 3:
                    or1.CommandText = "SELECT MAX(TXTIPIND) AS TXTIPIND FROM TXTIND WHERE TXTIMANDANT=:TXTIMANDANT:";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    SResult = Connect2Proxy(or1);
                    break;
                default:
                    break;
            }
            SResult.Tables[0].TableName = "TXTIND";
            return SResult;
        }
        public DataSet QTXTIND02(DataRow dwData)
        {

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT *  FROM TXTIND T  WHERE TXTIMANDANT=:TXTIMANDANT: AND TXTISIND=:TXTISIND: AND TXTITXTART=:TXTITXTART:"
                                  + " ORDER BY 1,2,3,4";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = "TXTIND";
            return SResult;
        }
        public DataSet QTXTIND99(DataRow dwData)
        {
            ;
            or1 = OracleDB.CreateCommand();

            or1.CommandText = "SELECT TXTITXTART, MAX(TXTIPIND) AS TXTIPIND FROM TXTIND WHERE TXTIMANDANT=:TXTIMANDANT:"
                + " GROUP BY TXTITXTART ORDER BY TXTIPIND DESC";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = "TXTIND";
            return SResult;
        }
        public void UpdateTXTIND(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            or1 = OracleDB.CreateCommand();
            switch (app.company)
            {
                default:
                    or1.CommandText = "UPDATE TXTIND SET TXTIMANDANT=:TXTIMANDANT:,TXTITXTART=:TXTITXTART:,TXTISIND=:TXTISIND:,TXTISPRACHKZ=:TXTISPRACHKZ:,TXTIPIND=:TXTIPIND:,TXTITITEL=:TXTITITEL:,TXTINURERGAENZ=:TXTINURERGAENZ:,TXTISAPTEXTART=:TXTISAPTEXTART:,TXTISTARTENDE=:TXTISTARTENDE:,TXTIKZPERS=:TXTIKZPERS:,TXTIAENDBEN=:TXTIAENDBEN:,TXTIAENDDAT=:TXTIAENDDAT:,TXTIAENDZL=:TXTIAENDZL:,TXTILPOS=:TXTILPOS:,TXTIUPDT=:TXTIUPDT:,TXTIRICHEDIT=:TXTIRICHEDIT:,TXTIFILLER=:TXTIFILLER:"
                       + " WHERE TXTIMANDANT=:TXTIMANDANT: AND TXTIPIND=:TXTIPIND:";
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    break;
            }
            Connect2Proxy(or1);
        }
        public void UTXT02(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE TXTIND SET TXTISIND = :TXTIFILLER: WHERE TXTISIND = :TXTISIND:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
    }
}
